/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.GroupDAO;
import exatrez.manager.GroupManager;
import exatrez.model.Groups;
import exatrez.model.InfoAudit;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.config.AuthRequired;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.RedirectResolution;

/**
 *
 * @author wan.fairul
 */
@UrlBinding("/adminGroup")
@AuthRequired
public class GroupAction extends BaseActionBean {

    private static Logger LOG = LoggerFactory.getLogger(GroupAction.class);
    private Groups group;
    private List<Groups> listOfGroup;
    @Inject
    private GroupManager groupManager;
    @Inject
    private GroupDAO groupDAO;

    @DefaultHandler
    public Resolution add_group() {

        group = new Groups();
        listOfGroup = groupManager.findAll();
        return new JSP("admin/group.jsp");
    }

    public Resolution saveGroup() {

        LOG.debug("masuk");
        Groups g = groupManager.findGroupName(group.getGroupName());
        if (g == null) {
            User user = getContext().getCurrentUser();
            InfoAudit ia = new InfoAudit();
            ia.setCreateDate(new Date());
            ia.setCreateId(user);
            group.setInfoAudit(ia);
            group.setActive('Y');
            groupManager.saveOrUpdate(group);
            addSimpleMessage("Success");
            LOG.debug("finish");
        } else {
            addSimpleError("Duplicate Group!");
        }
        return add_group();
    }

    public Resolution deleteGroup() {
        String groupId = getContext().getRequest().getParameter("groupId");
        if (groupId != null) {
            group = groupDAO.findById(Integer.parseInt(groupId));
            if (group != null) {
                groupManager.deleteGroup(group);
                addSimpleMessage("Success Delete");
            }
        }
        return new RedirectResolution(GroupAction.class, "add_group");
    }

    public Resolution popupEditGroup() {
        String groupId = getContext().getRequest().getParameter("groupId");
        if (groupId != null) {
            group = groupDAO.findById(Integer.parseInt(groupId));
        }
        return new JSP("admin/edit_group.jsp").addParameter("popup", "true");
    }

    public Resolution saveEditGroup() {
        String groupId = getContext().getRequest().getParameter("group.groupId");

        if (groupId != null) {
            User user = getContext().getCurrentUser();
            Groups g = groupDAO.findById(Integer.parseInt(groupId));
            InfoAudit ia = g.getInfoAudit();
            if (!group.getGroupName().equals(g.getGroupName())) {
                Groups g1 = groupManager.findGroupIsnotGroupName(String.valueOf(g.getGroupId()), group.getGroupName());
                if (g1 != null) {
                    addSimpleError("Duplicate Group Name!");
                    return add_group();
                } else {
                    g.setGroupName(group.getGroupName());
                }
            }
            g.setActive(group.getActive());
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            g.setInfoAudit(ia);
            groupManager.saveOrUpdate(g);
            addSimpleMessage("Success Update");
        }
        return new RedirectResolution(GroupAction.class, "add_group");
    }

    public Groups getGroup() {
        return group;
    }

    public void setGroup(Groups group) {
        this.group = group;
    }

    public GroupManager getGroupManager() {
        return groupManager;
    }

    public void setGroupManager(GroupManager groupManager) {
        this.groupManager = groupManager;
    }

    public List<Groups> getListOfGroup() {
        return listOfGroup;
    }

    public void setListOfGroup(List<Groups> listOfGroup) {
        this.listOfGroup = listOfGroup;
    }
}
